<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <title>Top Sellers</title>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" media="screen" type="text/css" title="Design" href="styles/design.css" />
</head>

<body>
    <!-- TITLE  -->
	<div id="page_title">
      <h1>Top <?php ?> Sellers</h1>
    </div>

    <!-- Side Menues -->
    <?php include "tools/Menues.html"; ?>

    <!-- Main Body -->
    <div id="page_body">
<?php
include "tools/Queries.php";

if(!isset($_GET['n'])) {
    $n = 100;
}
else {
    $n = $_GET['n'];
}

$sql = "SELECT S.supplierID, S.Name, sum(T.TotalCost) as TotalSpent\n " 
    . "FROM suppliers AS S\n"
    . "LEFT JOIN\n"
    . "\n"
    . "(\n"
    . " SELECT P.purchaseID,P.supplierID,sum(qty*cost) AS TotalCost FROM purchases AS P\n"
    . " LEFT JOIN itemsPurchased AS I\n"
    . " ON P.purchaseID = I.purchaseID\n"
    . " GROUP BY purchaseID\n"
    . ") AS T\n"
    . "\n"
    . "ON S.supplierID = T.supplierID\n"
    . "GROUP BY S.supplierID\n"
    . "ORDER BY TotalSpent DESC\n"
    . "LIMIT 0, $n ";
echo "\n<form action='top_seller.php' method='get'>";
echo "\nShow Top: <input type='type' name='n' value='$n' size=5/>";
echo "\n<input type='submit' name='submit' value='Go'>";
echo "\n</form>";

EchoSelectQuery($sql);

?>

    </div>
    
    <!-- Footer -->
    <?php include "tools/Footer.html"; ?>
</body>
</html>